package com.communal.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.communal.dao.ICommunalDao;
import com.communal.model.Model;
import com.communal.model.cr.CRWater;
import com.communal.model.tarif.Tarif;
import com.communal.model.tarif.TarifWaterCold;
import com.communal.model.tarif.TarifWaterHot;
import com.communal.util.AppConstants;
import com.communal.util.CommunalFilterBean;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Repository
public class CommunalDao extends BaseDao implements ICommunalDao{
	
	protected final String PROPERTY_TARIF = "tarif";
	protected final String PROPERTY_YEAR = "year";
	protected final String PROPERTY_MONTH = "month";
	protected final String PROPERTY_TARIF_NAME = "tarifName";
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public <T> List<T> getRecords(Model p_model, Class p_class, CommunalFilterBean p_filter) {
		DetachedCriteria criteria = DetachedCriteria.forClass(p_class)
		.add(Restrictions.eq(PROPERTY_MODEL, p_model))
		.add(Restrictions.eq(PROPERTY_ENABLED, AppConstants.ENTITY_STATE_ENABLED));
		
		if(p_filter != null){
			if(p_filter.getYear()!= null && !AppConstants.FAKE_ID.equals( p_filter.getYear().intValue() )){
				criteria.add( Restrictions.eq( PROPERTY_YEAR, p_filter.getYear() ) );
			}
			if(p_filter.getMonth()!= null && !AppConstants.FAKE_ID.equals( p_filter.getMonth().intValue() )){
				criteria.add( Restrictions.eq( PROPERTY_MONTH, p_filter.getMonth() ) );
			}
			//TODO implement filtration for date field
		}
		
		criteria.addOrder( Order.asc( PROPERTY_YEAR ) )
		.addOrder( Order.asc( PROPERTY_MONTH ) );
		return hibernateTemplate.findByCriteria(criteria);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public <T> List<T> getRecordsByTarif(Model p_model, Class p_class, Object p_tarif) {
		DetachedCriteria criteria = DetachedCriteria.forClass(p_class)
		.add(Restrictions.eq(PROPERTY_MODEL, p_model))
		.add(Restrictions.eq(PROPERTY_ENABLED, AppConstants.ENTITY_STATE_ENABLED))
		.addOrder(Order.asc(PROPERTY_ID));
		
		if(p_tarif != null){
			criteria.add( Restrictions.eq( PROPERTY_TARIF, p_tarif ));
		}
		
		return hibernateTemplate.findByCriteria(criteria);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<CRWater> getWaterRecordsByTarif(Model p_model, TarifWaterCold p_tarifWaterCold, TarifWaterHot p_tarifWaterHot) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CRWater.class)
		.add(Restrictions.eq(PROPERTY_MODEL, p_model))
		.add(Restrictions.eq(PROPERTY_ENABLED, AppConstants.ENTITY_STATE_ENABLED))
		.addOrder(Order.asc(PROPERTY_ID));
		
		if(p_tarifWaterCold!= null){
			criteria.add( Restrictions.eq( "tarifWaterCold", p_tarifWaterCold ));
		}
		
		if(p_tarifWaterHot!= null){
			criteria.add( Restrictions.eq( "tarifWaterHot", p_tarifWaterHot ));
		}
		
		return hibernateTemplate.findByCriteria(criteria);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Tarif> getEnabledTarifsByName(Model p_model, Class p_class, String p_name) {
		DetachedCriteria criteria = DetachedCriteria.forClass(p_class)
		.add(Restrictions.eq(PROPERTY_MODEL, p_model))
		.add(Restrictions.eq(PROPERTY_ENABLED, AppConstants.ENTITY_STATE_ENABLED))
		.add(Restrictions.eq( PROPERTY_TARIF_NAME, p_name ));
		List<Tarif> tarifList = hibernateTemplate.findByCriteria(criteria);
		return tarifList;
	}
	
}
